/**
 * 下拉请求太多，使用缓存记录到 cookie
 * cookie 60s过期
 * */
import { safeParseJson } from '@/utils/index';

export function cacheClear(key) {
  var uuid = key;
  sessionStorage.setItem(uuid, '');
}

export default function cacheAjaxWrapper(key, promiseFun) {
  var uuid = key;
  // console.log('缓存的key', uuid);
  

  var result = safeParseJson(sessionStorage.getItem(uuid));
  if (result/*有缓存就返回，没有重新请求数据*/) {
    // console.log('走了缓存，uuid', uuid, result);
    return Promise.resolve(result);
  }

  console.log('没有走缓存', uuid);
  return promiseFun().then(result => {
    /**默认是天
     * 5 / 24 / 60===>5分钟
     * */
    sessionStorage.setItem(uuid, JSON.stringify(result), { expires: 5 / 24 / 60 });
    return result;
  }).catch(e => {
    console.error(e);
    return undefined;
  });
}
